package backend.ro.msg.gillardon.persistence.orientdb;

import java.time.LocalDateTime;
import java.util.List;

import backend.ro.msg.gillardon.persistence.ApiInterface;

import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx;
import com.orientechnologies.orient.core.intent.OIntentMassiveInsert;
import com.orientechnologies.orient.core.record.impl.ODocument;
import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery;

public class DocumentApi implements ApiInterface {

  //  private static final Logger LOG = Logger.getLogger(DocumentApi.class);
  private final ODatabaseDocumentTx db;

  public DocumentApi() {
    this.db = new ODatabaseDocumentTx(DATABASE_URL);
  }

  public ODatabaseDocumentTx getConnection() {
    return this.db;
  }

  public void createRecord(final long COUNT) {
    this.db.open("msg", "msgGillardon");
    this.db.declareIntent(new OIntentMassiveInsert());
    ODocument doc = new ODocument("Article");

    for (int i = 0; i < COUNT; i++) {
      doc.reset();
      doc.setClassName("Article");
      LocalDateTime timePoint = LocalDateTime.now();
      doc.field("title", "Titlu NOU");
      doc.field("content", "come longer content");
      doc.field("whenPublished", timePoint);
      // SAVE THE DOCUMENT
      doc.save();
    }
    this.db.declareIntent(null);
    this.db.close();
    //DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  }

  @Override
  public void createRecord() {
    this.db.open("msg", "msgGillardon");
    LocalDateTime timePoint = LocalDateTime.now();
    ODocument doc = new ODocument("Article");
    doc.field("title", "Titlu NOU");
    doc.field("content", "come longer content");

    //DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    doc.field("whenPublished", timePoint);

    // SAVE THE DOCUMENT
    doc.save();
    //get the @rid
    //ORID rid = doc.getRecord().getIdentity();
    // OIdentifiable rec = db.load(new ORecordId(rid));
    //ODocument doc1 = rec.getRecord();
    // orientdb editor doesn't show the date properly
    //    LOG.debug(doc1.field("whenPublished"));

    this.db.close();
  }

  @Override
  public void updateRecord() {
    this.db.open("msg", "msgGillardon");
    String titleToBeUpdated = "Titlu NOU";
    List<ODocument> result =
      this.db.query(new OSQLSynchQuery<ODocument>("select * from Article where title='" + titleToBeUpdated + "'"));
    for (ODocument article : result) {
      article.field("title", "Titlu docApi");
      // SAVE THE DOCUMENT
      article.save();

    }
    this.db.close();
  }

  @Override
  public void deleteRecord() {
    this.db.open("msg", "msgGillardon");
    String titleToBeDeleted = "Titlu docApi";
    List<ODocument> result =
      this.db.query(new OSQLSynchQuery<ODocument>("select * from Article where title='" + titleToBeDeleted + "'"));
    for (ODocument article : result) {
      article.delete();
    }
    this.db.close();
  }
}
